﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using NUnit.Framework;
using DBSourceToolsLib;
using DBConnectionLib;

namespace TestDBSourceToolsLibNUnit
{
    [TestFixture]
    [Ignore]
    public class TestDeploymentTargetCommand
    {
        private const string strControlPath = BaseDirectoryHelper.Unused + "\\control\\testDeploymentTarget";
        private const string strOutputPath = BaseDirectoryHelper.Unused + "\\output";
        private const string strScriptControlPath = BaseDirectoryHelper.Unused + "\\control";

        [TestFixtureSetUp]
        public void Init()
        {
            FileHelper oFileHelper = new FileHelper();
            oFileHelper.DeleteFilesInDir(strOutputPath);

            Database oDatabase = new Database("PatternsDB", ".", "PatternsDB", strControlPath);
            oDatabase.RunScript(strScriptControlPath + "\\CreateTableViewProcFn_Cleanup.sql");
            oDatabase.RunScript(strScriptControlPath + "\\CreateTableViewProcFn.sql");
            oDatabase.RunScript(strScriptControlPath + "\\PatternsDBDeployed_DropDB.sql");


        }

        [Test]
        public void TestNewCommand()
        {
            
            //DBSourceToolsLib.Commands.DeploymentTargetCommand oNewCmd =
            //    new DBSourceToolsLib.Commands.DeploymentTargetCommand(DBSourceToolsLib.Commands.DeploymentTargetCommandTypes.NewTarget, null);

            //Database oDatabase = new Database("PatternsDB", ".", "PatternsDB", strControlPath);

            //Database oNewDatabase = new Database("newDB", ".", "newDB", strControlPath);
            //DBSourceToolsLib.SysObjects.SysObjects sysObjects = new DBSourceToolsLib.SysObjects.SysObjects(oDatabase, oNewDatabase.BaseDirectory);
            //oNewCmd.Execute(ref oDatabase, oNewDatabase, null, sysObjects);

            //Database oAddedDB = sysObjects.DeploymentTargets.getDatabase("newDB");
            //Assert.IsNotNull(oAddedDB, " New Target Failed to create database");
            //Assert.IsTrue(oAddedDB.IsDeploymentTarget, " Database should be marked as a Deployment target");
            //Assert.IsTrue(oAddedDB.BaseDirectory.CompareTo(strControlPath + "\\PatternsDB\\DeploymentTargets\\newDB") == 0,
            //    " Base Directory not set correctly");

        }

        [Test]
        public void TestSaveCommand()
        {
            
            //DBSourceToolsLib.Commands.DeploymentTargetCommand oNewCmd =
            //    new DBSourceToolsLib.Commands.DeploymentTargetCommand(
            //        DBSourceToolsLib.Commands.DeploymentTargetCommandTypes.NewTarget, null);

            //Database oDatabase = new Database("PatternsDB", ".", "PatternsDB", strControlPath);
            //Database oNewDatabase = new Database("newDB", ".", "newDB", strControlPath);

            //DBSourceToolsLib.SysObjects.SysObjects sysObjects = new DBSourceToolsLib.SysObjects.SysObjects(oDatabase
            //    , oNewDatabase.BaseDirectory);
            //oNewCmd.Execute(ref oDatabase, oNewDatabase, null, sysObjects);
            //oNewDatabase.DataSourceName = "newDS";
            //oNewDatabase.DatabaseName = "newDBName";

            //DBSourceToolsLib.Commands.DeploymentTargetCommand oSaveCommand =
            //    new DBSourceToolsLib.Commands.DeploymentTargetCommand(
            //        DBSourceToolsLib.Commands.DeploymentTargetCommandTypes.SaveTarget, null);

            //oSaveCommand.Execute(ref oDatabase, oNewDatabase, null, sysObjects);

            //Database oUpdatedDB = sysObjects.DeploymentTargets.getDatabase("newDB");
            //Assert.IsNotNull(oUpdatedDB, " New Target Failed to create database");

            //Assert.IsTrue(oUpdatedDB.DataSourceName.CompareTo("newDS") == 0, "DataSource name incorrect after update");
            //Assert.IsTrue(oUpdatedDB.DatabaseName.CompareTo("newDBName") == 0, "DBName incorrect after update");

        }

        [Test]
        public void TestRenameCommand()
        {
            //DBSourceToolsLib.Commands.DeploymentTargetCommand oNewCmd =
            //    new DBSourceToolsLib.Commands.DeploymentTargetCommand(
            //        DBSourceToolsLib.Commands.DeploymentTargetCommandTypes.NewTarget, null);

            //Database oDatabase = new Database("PatternsDB", ".", "PatternsDB", strControlPath);
            //Database oNewDatabase = new Database("newDB", ".", "newDB", strControlPath);

            //DBSourceToolsLib.SysObjects.SysObjects sysObjects = new DBSourceToolsLib.SysObjects.SysObjects(oDatabase
            //    , oNewDatabase.BaseDirectory);
            //oNewCmd.Execute(ref oDatabase, oNewDatabase, null, sysObjects);
            //oNewDatabase.DataSourceName = "newDS";
            //oNewDatabase.DatabaseName = "newDBName";

            //DBSourceToolsLib.Commands.DeploymentTargetCommand oSaveCommand =
            //    new DBSourceToolsLib.Commands.DeploymentTargetCommand(
            //        DBSourceToolsLib.Commands.DeploymentTargetCommandTypes.RenameTarget, null);

            //oSaveCommand.Execute(ref oDatabase, oNewDatabase, "renamedTarget", sysObjects);

            //Database oUpdatedDB = sysObjects.DeploymentTargets.getDatabase("renamedTarget");
            //Assert.IsNotNull(oUpdatedDB, " Rename failed to rename Target");

            //Assert.IsTrue(oUpdatedDB.DataSourceName.CompareTo("newDS") == 0, "DataSource name incorrect after update");
            //Assert.IsTrue(oUpdatedDB.DatabaseName.CompareTo("newDBName") == 0, "DBName incorrect after update");
            //Assert.IsTrue(oUpdatedDB.BaseDirectory.CompareTo(strControlPath + "\\PatternsDB\\DeploymentTargets\\renamedTarget") == 0,
            //    " Base Directory not set correctly");



        }

        [Test]
        public void TestDeleteCommand()
        {
            //DBSourceToolsLib.Commands.DeploymentTargetCommand oNewCmd =
            //    new DBSourceToolsLib.Commands.DeploymentTargetCommand(DBSourceToolsLib.Commands.DeploymentTargetCommandTypes.NewTarget, null);

            //Database oDatabase = new Database("PatternsDB", ".", "PatternsDB", strControlPath);
            //Database oNewDatabase = new Database("newDB", ".", "newDB", strControlPath);
            //DBSourceToolsLib.SysObjects.SysObjects sysObjects = new DBSourceToolsLib.SysObjects.SysObjects(oNewDatabase
            //    , oNewDatabase.BaseDirectory);
            //oNewCmd.Execute(ref oDatabase, oNewDatabase, null, sysObjects);

            //DBSourceToolsLib.Commands.DeploymentTargetCommand oSaveCommand =
            //    new DBSourceToolsLib.Commands.DeploymentTargetCommand(
            //        DBSourceToolsLib.Commands.DeploymentTargetCommandTypes.DeleteTarget, null);

            //oSaveCommand.Execute(ref oDatabase, oNewDatabase, null, sysObjects);

            //Assert.IsTrue(sysObjects.DeploymentTargets.Count == 0, "Delete unsuccessful");

        }

        [Test]
        public void TestSaveDatabaseListToDiskCommand()
        {
            //DBSourceToolsLib.Commands.DeploymentTargetCommand oNewCmd =
            //    new DBSourceToolsLib.Commands.DeploymentTargetCommand(
            //        DBSourceToolsLib.Commands.DeploymentTargetCommandTypes.NewTarget, null);

            //Database oDatabase = new Database("PatternsDB", ".", "PatternsDB", strOutputPath);
            //Database oNewDatabase = new Database("newDB", "newDS", "newDBName", strOutputPath);

            //DBSourceToolsLib.SysObjects.SysObjects sysObjects = new DBSourceToolsLib.SysObjects.SysObjects(oNewDatabase
            //    , oNewDatabase.BaseDirectory);
            //oNewCmd.Execute(ref oDatabase, oNewDatabase, null, sysObjects);

            //DBSourceToolsLib.Commands.DeploymentTargetCommand oSaveCommand =
            //    new DBSourceToolsLib.Commands.DeploymentTargetCommand(
            //        DBSourceToolsLib.Commands.DeploymentTargetCommandTypes.SaveToDisk, null);

            //oSaveCommand.Execute(ref oDatabase, null, null, sysObjects);

            //DBSourceToolsLib.FileComparer.FileComparer oFileComparer
            //    = new DBSourceToolsLib.FileComparer.FileComparer(
            //        strControlPath + "\\PatternsDB\\DeploymentTargets\\Databases.xml",
            //        strOutputPath + "\\PatternsDB\\DeploymentTargets\\Databases.xml");

            //Assert.IsTrue(oFileComparer.AreFilesEqual(), " Invalid " + strOutputPath + "\\PatternsDB\\DeploymentTargets\\Databases.xml");

        }

        [Test]
        public void TestSaveScriptCommand()
        {
//            DBSourceToolsLib.Commands.DeploymentTargetCommand oNewCmd =
//                new DBSourceToolsLib.Commands.DeploymentTargetCommand(
//                    DBSourceToolsLib.Commands.DeploymentTargetCommandTypes.NewTarget, null);

//            Database oDatabase = new Database("PatternsDB", ".", "PatternsDB", strOutputPath);
//            Database oNewDatabase = new Database("newDB", "newDS", "newDBName", strOutputPath);

//            DBSourceToolsLib.SysObjects.SysObjects sysObjects = new DBSourceToolsLib.SysObjects.SysObjects(oNewDatabase
//                , oNewDatabase.BaseDirectory);
//            oNewCmd.Execute(ref oDatabase, oNewDatabase, null, sysObjects);
//            //oNewDatabase.DataSourceName = "newDS";
//            //oNewDatabase.DatabaseName = "newDBName";

//            //DBSourceToolsLib.Commands.DeploymentTargetCommand oSaveCommand =
//            //    new DBSourceToolsLib.Commands.DeploymentTargetCommand(
//            //        DBSourceToolsLib.Commands.DeploymentTargetCommandTypes.SaveTarget, null);

//            //oSaveCommand.Execute(ref oDatabase, oNewDatabase, null);

//            //Database oUpdatedDB = sysObjects.DeploymentTargets.getDatabase("newDB");
//            //Assert.IsNotNull(oUpdatedDB, " New Target Failed to create database");

//            //Assert.IsTrue(oUpdatedDB.DataSourceName.CompareTo("newDS") == 0, "DataSource name incorrect after update");
//            //Assert.IsTrue(oUpdatedDB.DatabaseName.CompareTo("newDBName") == 0, "DBName incorrect after update");

//            DBSourceToolsLib.Commands.DeploymentTargetCommand oSaveToDisk =
//                new DBSourceToolsLib.Commands.DeploymentTargetCommand(
//                    DBSourceToolsLib.Commands.DeploymentTargetCommandTypes.SaveToDisk, null);

//            oSaveToDisk.Execute( ref oDatabase, null, null, sysObjects);

//            FileHelper oFileHelper = new FileHelper();
//            string sourceDir = strControlPath + "\\PatternsDB";
//            string targetDir = strOutputPath + "\\PatternsDB";

//            oFileHelper.CompareDirectories(sourceDir, targetDir);

////            string strCreateScript = oUpdatedDB.CreateScript.ScriptText;


        }

        //[Test]
        //public void TestDeployTarget()
        //{
        //    DBSourceToolsLib.Commands.DeploymentTargetCommand oNewCmd =
        //        new DBSourceToolsLib.Commands.DeploymentTargetCommand(DBSourceToolsLib.Commands.DeploymentTargetCommandTypes.NewTarget, null);

        //    Database oDatabase = new Database("PatternsDB", ".", "PatternsDB", strOutputPath + "\\testDeployment");

        //    System.Windows.Forms.Form oForm = new System.Windows.Forms.Form();

        //    DBSourceToolsLib.Commands.DatabaseCommand oDBCommand =
        //        new DBSourceToolsLib.Commands.DatabaseCommand(oForm,
        //            DBSourceToolsLib.Commands.DatabaseCommandTypes.LoadFromDatabase);

        //    oDBCommand.Execute(ref oDatabase);


        //    Database oNewDatabase = new Database("deployPatternsDB", ".", "PatternsDBDeployed", null);

        //    oNewCmd.Execute(ref oDatabase, oNewDatabase, null);

        //    DBSourceToolsLib.Commands.DatabaseCommand oDBWriteToDisk =
        //        new DBSourceToolsLib.Commands.DatabaseCommand(oForm,
        //            DBSourceToolsLib.Commands.DatabaseCommandTypes.WriteToDisk);

        //    oDBWriteToDisk.Execute(ref oDatabase);
        //    Assert.Fail("Test Incomplete");


        //}

    }
}
